﻿@using DAL.Helpers;

@model Web.Models.FilmeViewModel

@{
    var title = "Cadastro de Filme";
    ViewBag.Title = title;
}

<h2>@title</h2>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>@title</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.TipoMidia)
        </div>
        <div class="editor-field">
            @{
                var tipoMidiaList = new Dictionary<short, string>();
                tipoMidiaList.Add((short)TipoMidia.Fisica, Enum.GetName(typeof(TipoMidia), TipoMidia.Fisica));
                tipoMidiaList.Add((short)TipoMidia.Digital, Enum.GetName(typeof(TipoMidia), TipoMidia.Digital));
            }

            @Html.DropDownList("TipoMidia", new SelectList(tipoMidiaList, "Key", "Value"))
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Titulo)
        </div>
        <div class="editor-field textSearch">
            @Html.EditorFor(model => model.Titulo)
            @Html.ValidationMessageFor(model => model.Titulo)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Descricao)
        </div>
        <div class="editor-field">
            @Html.TextAreaFor(model => model.Descricao, new { @class = "textDescription" })
            @Html.ValidationMessageFor(model => model.Descricao)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.UrlImagem)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.UrlImagem)
            @Html.ValidationMessageFor(model => model.UrlImagem)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.UrlTrailer)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.UrlTrailer)
            @Html.ValidationMessageFor(model => model.UrlTrailer)
        </div>
        
        @Html.HiddenFor(model => model.TmdbId, new { @class="tmdbId" })

        <p>
            <input type="submit" value="Salvar" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Voltar para Meus Filmes", "Index")
</div>

@section Scripts {
    @Styles.Render("~/Content/themes/base/css")
    @Scripts.Render("~/bundles/jqueryval")
    @Scripts.Render("~/bundles/jqueryui")
    
    <script type="text/javascript">
        $(function () {
            $.ui.autocomplete.prototype._renderItem = function (ul, item) {
                item.label = item.label.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + $.ui.autocomplete.escapeRegex(this.term) + ")(?![^<>]*>)(?![^&;]+;)", "gi"), "<strong>$1</strong>")

                return $("<li></li>")
                    .data("item.autocomplete", item)
                    .append("<a>" + item.label + "</a>")
                    .appendTo(ul);
            };

            $(".textSearch > input[type='text']").autocomplete({
                delay: 300,
                minLength: 3,
                source: function (request, response) {
                    var t = $(".textSearch > input[type='text']").val();
                    $.post("@Url.Action("GetMovies")", { titulo: t }, function (data) {
                        var res = [];
                        $(data).each(function (i, elem) {
                            res.push({ label: elem.Title, value: elem.Title, id: elem.Id });
                        });

                        response(res);
                    });
                },
                select: function (event, ui) {
                    $(".tmdbId").val(ui.item.id);
                    $.post("GetMovie", { id: ui.item.id }, function (data) {
                        $("#Descricao").val(data.Overview);
                        $("#UrlImagem").val(data.PosterPath);
                    });

                    return false;
                }
            });
        });
    </script>
}